<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Auth
	{
		private $_ci;
		function __construct(){
			$this->_ci =& get_instance();
			$this->_ci->load->model('user_model','',TRUE);
		}	

		//生成左侧菜单
		function render_menu()
		{
			//$father_module_tpl = "<li>%s</li>";
			//$child_module_tpl= '<ul class="sub-menu"></ul>';
			//获取用户id
			$uid = $this->get_user_id();
			//获取用户信息
			$user =  $this->_ci->user_model->get_user_by_id($uid);

			if(!empty($uid))
			{
				$html = '';
				//获取拥有的父模块权限
				$father_modules = $this->_ci->user_model->get_group_modules($user['group'],0);

				foreach ($father_modules  as $key => $father) {

					$html .= '<li>';

					//获取该父模块下拥有全新的子模块
					$icon = '';
		
					if($father['module_icon'])
					{
						$icon = '<i class="fa fa-'.$father['module_icon'].'"></i>';
					}


					$html .= '<a href="javascript:;">'.$icon.'<span class="title">'.$father['module_name'].'</span></a>';

					//取出改模块下的子模块
					$children_modules = $this->_ci->user_model->get_group_modules($user['group'],$father['module_id']);

					if($children_modules)
					{
						$html .= '<ul class="sub-menu">';

						foreach ($children_modules as $key => $children) 
						{
							$icon = '';

							if($children['module_icon'])
							{
								$icon = '<i class="fa fa-'.$children['module_icon'].'"></i>';
							}

							$html .= '<li><a href="'.$children['module_url'].'">'.$icon.$children['module_name'].'</a></li>';
						}

						$html .= '</ul>';
					}

					$html .= '</li>';
				}
		
				echo $html;
			}
		}

		//检查
		function has_permission($module_url,$action_url=NULL)
		{
			//获取用户id
			$uid = $this->get_user_id();
			//获取用户信息
			$user =  $this->_ci->user_model->get_user_by_id($uid);

			//查找相应的URL
			$module = $this->_ci->db->get_where('modules',array('module_url'=>$module_url))->row_array();

			if($module)
			{
				//验证Module是否有权限
				$has_module = $this->_ci->db->get_where('group_permissions',array('group_id'=>$user['group'],'permission_id'=>$module['module_id'],'type'=>'module'))->row_array();
				if(!empty($has_module))
				{
					if($action_url)
					{
						$action = $this->_ci->db->get_where('module_actions',array('action_url'=>$action_url))->row_array();

						if($action)
						{
							$has_action = $this->_ci->db->get_where('group_permissions',array('group_id'=>$user['group'],'permission_id'=>$action['action_id'],'type'=>'action'))->row_array();
							if(!empty($has_action))
							{
								return TRUE;
							}
						} 
					} else {
							return TRUE;
						}
				}
			}

			return FALSE;						
		}

		function insert_user_action_logs($module,$action,$content)
	    {
	    	$uid = $this->get_user_id();

	    	$data = array('adminer_id'=>$uid,
	    				'module'=>$module,
	    				'action'=>$action,
	    				'content'=>serialize($content),
	    				'create_time'=>time());

	    	$this->_ci->db->insert('adminer_action_logs',$data);
	    }

		private function get_user_id()
		{
			$user_id = $this->_ci->session->userdata('user_id');
			if (!empty($user_id))
			{
				return $user_id;
			}
			return null;
		}
	}



?>